﻿using ERPSystem.Common.Domains.Delivergoods;
using ERPSystem.Common.Domains.ReturnNotice;
using ERPSystem.Common.DTO;
using ERPSystem.Common.Infrastructure.IRepository;
using MediatR;
using Microsoft.AspNetCore.StaticFiles;
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel;
using SalesManagementCommon.API.SalesManagementCommons.Shippingnotess;

namespace SalesManagementCommon.API.SalesManagementCommons.ReturnNotices
{
    public class ReturnNoticesExcelHandler:IRequestHandler<ReturnNoticesExcelCommand, FileExportResult>
    {
        private readonly IBaseRepository<refundNotice> refundNoticeRepository;

        public ReturnNoticesExcelHandler(IBaseRepository<refundNotice> refundNoticeRepository)
        {
            this.refundNoticeRepository = refundNoticeRepository;
        }


        /// <summary>
        /// 退货通知单导出
        /// </summary>
        /// <param name="request"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public async Task<FileExportResult> Handle(ReturnNoticesExcelCommand request, CancellationToken cancellationToken)
        {
            var result = new FileExportResult();
            var list = await (await refundNoticeRepository.GetAll()).ToListAsync();
            Console.WriteLine(list.Count);
            IWorkbook book = new XSSFWorkbook();
            ISheet sheet = book.CreateSheet();
            IRow row1 = sheet.CreateRow(0);

            row1.CreateCell(0).SetCellValue("序号");
            row1.CreateCell(1).SetCellValue("单据编码");
            row1.CreateCell(2).SetCellValue("单据日期");
            row1.CreateCell(3).SetCellValue("单据状态");
            row1.CreateCell(4).SetCellValue("业务状态");
            row1.CreateCell(5).SetCellValue("业务类型");
            row1.CreateCell(6).SetCellValue("操作员");
            row1.CreateCell(7).SetCellValue("仓库");
            row1.CreateCell(8).SetCellValue("客户名称");
            row1.CreateCell(9).SetCellValue("退货原因");
            row1.CreateCell(10).SetCellValue("处理方式");
            row1.CreateCell(11).SetCellValue("备注信息");



            int rowIndex = 1;
            foreach (var item in list)
            {
                row1 = sheet.CreateRow(rowIndex);
                row1.CreateCell(0).SetCellValue(item.ReturnNoticesId);
                row1.CreateCell(1).SetCellValue(item.ReturnNumber);
                row1.CreateCell(2).SetCellValue(item.DocumentsDate);
                row1.CreateCell(3).SetCellValue(item.Documentstatus);
                row1.CreateCell(4).SetCellValue(item.BusinessstateId);
                row1.CreateCell(5).SetCellValue(item.BusinessTypeId);
                row1.CreateCell(6).SetCellValue(item.Operator);
                row1.CreateCell(7).SetCellValue(item.Warehouseld);
                row1.CreateCell(8).SetCellValue(item.ClientName);
                row1.CreateCell(9).SetCellValue(item.ReturnReason);
                row1.CreateCell(10).SetCellValue(item.Processing);
                row1.CreateCell(11).SetCellValue(item.Remark);

                rowIndex++;

            }
            byte[] data = null;
            using (MemoryStream ms = new MemoryStream())
            {
                book.Write(ms);
                data = ms.ToArray();
            }
            string contextType = new FileExtensionContentTypeProvider().Mappings[".xlsx"];
            string fileName = "退货通知单.xlsx";

            result.FileBytes = data;
            result.MimeType = contextType;
            result.FileName = fileName;
            return result;
        }
      
    }
}
